package cn.com.cennavi.fileMake;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Set;
import java.util.TreeMap;

import cn.com.cennavi.bean.CloseRoadMapData;
import cn.com.cennavi.bean.DifPointTotalMapInfo;
import cn.com.cennavi.bean.R_G_DATA;
import cn.com.cennavi.util.MapUtil;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.operation.linemerge.LineMerger;

/**
 * 
 * 
 * @ClassName: 地面道路地图编译
 * @Description: TODO（这里用一句话描述这个类的作用）
 * @author bull
 * @date 2016年11月11日 上午10:07:14
 *
 */
public class GroundRoadMapConfigCompile {
	private static double bufferAreaDistance = 0.00020;// 缓冲区半径10米
	private static final String R_G_Map_File = "D:\\data\\R-G\\R-G_16map_beijing.csv";
	// public static final String inputFileFolder =
	// "D:\\data\\TrafficLightCrossMap\\TrafficLightCrossMap";
	// public static final String closeRoadMapDataFile =
	// "D:\\data\\TrafficLightCrossMap_shanghai\\combine.csv";
	public static final String closeRoadMapDataFile = "D:\\data\\TrafficLightCrossMap_beijingGroundRoad\\combine1.csv";
	// public static final String closeRoadMapDataFile =
	// "C:\\Users\\Thinkpad\\Downloads\\part-00003";
	// public static final String closeRoadTurnAttrFile =
	// "d:\\data\\compileResult_16G_MAP_groundRoad_TurnAttr.csv";
	// public static final String closeRoadLinkLengthFile =
	// "d:\\data\\lonlat_16G_groundRoad_linkLength.csv";
	// public static final String outFile =
	// "d:\\data\\DifPointTotalMapInfo_closeRoad_10mibuf.csv";
	public static final String outFile = "d:\\data\\DifPointTotalMapInfo_groundRoad_beijing_20mibuf.csv";
	public static final int cellSize = 200;
	public static final int simpleSmallNum = 180;
	public static final int roadClass = 1;
	public static void main(String[] args) throws IOException, ParseException {
		// TODO Auto-generated method stub

		// 筛选所有地面道路包含了左右转的top50个红绿灯数据
		// HashMap<String, CloseRoadMapData> dataMap =
		// getZhixingRoadMap("D:\\data\\TrafficLightCrossMap_beijingGroundRoad\\combine.csv",
		// 2640);
		// sortRoad2("D:\\data\\TrafficLightCrossMap_beijingGroundRoad\\combine.csv",
		// 50, "d:\\data\\DifPointTotalMapInfo_groundRoad_beijing_20mibuf.csv",
		// dataMap);
		/**
		 * 1.link固有的地图属性
		 */

		// 得到link的形状 和长度信息和meshid信息
		HashMap<String, Geometry> linkGeo = new HashMap<String, Geometry>();

		// 得到link的方位角范围
		HashMap<String, String> linkAngle = new HashMap<String, String>();

		// link的meshiD
		HashMap<String, Integer> linklen = new HashMap<String, Integer>();

		// link的长度信息
		HashMap<String, String> linkMeshIds = new HashMap<String, String>();

		getLinkGeo(R_G_Map_File, linkGeo, linklen, linkMeshIds, cellSize, linkAngle);

		/**
		 * 2.操作封闭道路分歧点数据 key:nodeID value:
		 */
		HashMap<String, ArrayList<DifPointTotalMapInfo>> sameNodeRoadsMap = new HashMap<String, ArrayList<DifPointTotalMapInfo>>();
//		HashMap<String, ArrayList<DifPointTotalMapInfo>> sameNodeRoadsMap1 = new HashMap<String, ArrayList<DifPointTotalMapInfo>>();

		// 分叉口道路按照nodeid分组--sameNodeRoadsMap
		getSameNodeRoads(closeRoadMapDataFile, sameNodeRoadsMap, simpleSmallNum,roadClass);

		// 按照nodeid+进出口link分组,并设置同行标志
		// 这一步修改了
		HashMap<String, ArrayList<DifPointTotalMapInfo>> node_in_out_map = getInOrOutInfo(sameNodeRoadsMap);

		// 获取相同方向上有分叉的道路的公共部分，得到分叉道路最后相同的linkid
		getNode_in_out_map(node_in_out_map);

		// 开始计算长度信息和形状信息以及
		getLinkLenDisGeoInfo(node_in_out_map, linklen, linkGeo, linkMeshIds, linkAngle);

		// 输出node_in_out_map
		outToFile(node_in_out_map, outFile);


	}

	/**
	 * 每个路口四个方向、直行的道路link序列多条，去样本数量多的那条
	 * 
	 * @param maxNum
	 * 
	 * @param string
	 * @param i
	 * @return
	 * @throws IOException
	 */
	private static HashMap<String, CloseRoadMapData> getZhixingRoadMap(String outfile2, int maxNum) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(outfile2)));
		String line = "";
		HashMap<String, HashMap<String, ArrayList<CloseRoadMapData>>> dataMap1 = new HashMap<String, HashMap<String, ArrayList<CloseRoadMapData>>>();
		HashMap<String, TreeMap<Integer, CloseRoadMapData>> datamap2 = new HashMap<String, TreeMap<Integer, CloseRoadMapData>>();
		while ((line = br.readLine()) != null) {
			CloseRoadMapData closeRoadMapData = CloseRoadMapData.parseData(line);
			if (closeRoadMapData.flag != 0) {
				continue;
			}
			int angle = closeRoadMapData.angel;
			int simpleNumber = closeRoadMapData.simpleNum;
			int flag = closeRoadMapData.flag;
			// String key = flag + "_" + angle;
			String key = closeRoadMapData.nodeId + "_" + closeRoadMapData.inRoadId;
			if (datamap2.containsKey(key)) {
				TreeMap<Integer, CloseRoadMapData> value = datamap2.get(key);
				value.put(simpleNumber, closeRoadMapData);
			} else {
				TreeMap<Integer, CloseRoadMapData> value = new TreeMap<Integer, CloseRoadMapData>();
				value.put(simpleNumber, closeRoadMapData);
				datamap2.put(key, value);
			}

		}
		HashMap<String, CloseRoadMapData> dataMap = new HashMap<String, CloseRoadMapData>();
		TreeMap<Integer, ArrayList<CloseRoadMapData>> dm = new TreeMap<Integer, ArrayList<CloseRoadMapData>>();
		for (Map.Entry<String, TreeMap<Integer, CloseRoadMapData>> s : datamap2.entrySet()) {
			TreeMap<Integer, CloseRoadMapData> ss = s.getValue();
			CloseRoadMapData dd = ss.lastEntry().getValue();
			int simpleNum = dd.simpleNum;
			if (dm.containsKey(simpleNum)) {
				dm.get(simpleNum).add(dd);
			} else {
				ArrayList<CloseRoadMapData> dl = new ArrayList<CloseRoadMapData>();
				dl.add(dd);
				dm.put(simpleNum, dl);
			}

		}
		NavigableMap<Integer, ArrayList<CloseRoadMapData>> descDm = dm.descendingMap();
		int count = 0;
		for (Map.Entry<Integer, ArrayList<CloseRoadMapData>> e : descDm.entrySet()) {
			ArrayList<CloseRoadMapData> list = e.getValue();
			for (CloseRoadMapData dd : list) {
				if (dataMap.containsKey(dd.nodeId + "_" + dd.inRoadId)) {
					System.out.println("hahhahah");
				}
				count++;
				// System.out.println(count);
				// dataMap.put(dd.nodeId + "_" + dd.inRoadId, dd);
				if (count <= maxNum)
					dataMap.put(dd.nodeId + "_" + dd.inRoadId, dd);
			}

		}
		return dataMap;
	}

	/**
	 * 
	 * @param dataMap
	 * @param string
	 * @param i
	 * @param string2
	 * @throws IOException
	 */
	private static void sortRoad2(String outfile2, int maxnum, String readfile, HashMap<String, CloseRoadMapData> dataMap) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(outfile2)));
		String line = "";

		// 获取到与直行有相同进入link的link序列
		HashMap<String, TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>>> dataMap3 = new HashMap<String, TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>>>();
		HashMap<String, TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>>> dataMap4 = new HashMap<String, TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>>>();
		HashMap<String, TreeMap<Integer, CloseRoadMapData>> r1 = new HashMap<String, TreeMap<Integer, CloseRoadMapData>>();
		for (Map.Entry<String, CloseRoadMapData> ss : dataMap.entrySet()) {
			String node = ss.getKey().split("\\_")[0];
			if (r1.containsKey(node)) {
				TreeMap<Integer, CloseRoadMapData> t1 = r1.get(node);
				t1.put(ss.getValue().angel, ss.getValue());
			} else {
				TreeMap<Integer, CloseRoadMapData> t1 = new TreeMap<Integer, CloseRoadMapData>();
				t1.put(ss.getValue().angel, ss.getValue());
				r1.put(node, t1);
			}
		}
		while ((line = br.readLine()) != null) {
			CloseRoadMapData dd = CloseRoadMapData.parseData(line);
			// 掉头的去掉
			if (dd.flag == 3) {
				continue;
			}
			// 先找每个分歧点4个方向上，直行数量最多的
			String key = dd.nodeId + "_" + dd.inRoadId;
			String key1 = dd.nodeId;
			int angle = dd.angel;
			// 先把与直行道路有相同inroadid的左转和右转筛选出来
			int flag = dd.flag;

			if (dataMap.containsKey(key) && flag == 4) {
				if (dataMap3.containsKey(key1)) {
					TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>> dm1 = dataMap3.get(key1);
					if (dm1.containsKey(angle)) {
						dm1.get(angle).put(dd.simpleNum, dd);
					} else {
						TreeMap<Integer, CloseRoadMapData> tm = new TreeMap<Integer, CloseRoadMapData>();
						tm.put(dd.simpleNum, dd);
						dm1.put(angle, tm);
					}
				} else {
					TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>> dm1 = new TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>>();
					TreeMap<Integer, CloseRoadMapData> dms = new TreeMap<Integer, CloseRoadMapData>();
					dms.put(dd.simpleNum, dd);
					dm1.put(angle, dms);
					dataMap3.put(key1, dm1);
				}
			}
			if (dataMap.containsKey(key) && flag == 5) {
				if (dataMap4.containsKey(key1)) {
					TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>> dm1 = dataMap4.get(key1);
					if (dm1.containsKey(angle)) {
						dm1.get(angle).put(dd.simpleNum, dd);
					} else {
						TreeMap<Integer, CloseRoadMapData> tm = new TreeMap<Integer, CloseRoadMapData>();
						tm.put(dd.simpleNum, dd);
						dm1.put(angle, tm);
					}
				} else {
					TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>> dm1 = new TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>>();
					TreeMap<Integer, CloseRoadMapData> dms = new TreeMap<Integer, CloseRoadMapData>();
					dms.put(dd.simpleNum, dd);
					dm1.put(angle, dms);
					dataMap4.put(key1, dm1);
				}
			}
		}

		// 对于相同enterlink和node、相同方向的link序列，取样本数最多的那
		// 筛选出来的数据取数据量最大的那个
		Set<String> noad_inlinks = r1.keySet();
		for (String noad_inlink : noad_inlinks) {
			String nodeId = noad_inlink;
			TreeMap<Integer, CloseRoadMapData> zhixnigLlist = r1.get(nodeId);
			if (dataMap3.containsKey(nodeId) && dataMap4.containsKey(nodeId)) {

				if (dataMap3.get(nodeId).size() >= 4 && dataMap4.get(nodeId).size() >= 4) {
					TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>> s3 = dataMap3.get(nodeId);
					TreeMap<Integer, TreeMap<Integer, CloseRoadMapData>> s4 = dataMap4.get(nodeId);

					int lastAngle3 = s3.lastEntry().getKey();
					int lastsimpleNum3 = s3.lastEntry().getValue().lastEntry().getValue().simpleNum;
					HashMap<Integer, CloseRoadMapData> dhd3 = new HashMap<Integer, CloseRoadMapData>();
					for (Map.Entry<Integer, TreeMap<Integer, CloseRoadMapData>> e3 : s3.entrySet()) {
						Integer nowangle3 = e3.getKey();
						// System.out.println(nowangle3);
						CloseRoadMapData data = e3.getValue().lastEntry().getValue();
						int nowsimpleNum3 = data.simpleNum;
						if (Math.abs(lastAngle3 - nowangle3) < 20 || Math.abs(lastAngle3 - nowangle3) > 340) {
							if (nowsimpleNum3 > lastsimpleNum3) {
								dhd3.remove(lastAngle3);
								dhd3.put(nowangle3, data);
							}
						} else {
							dhd3.put(nowangle3, data);
						}
						lastAngle3 = nowangle3;
						lastsimpleNum3 = nowsimpleNum3;
					}

					int lastAngle4 = s4.lastEntry().getKey();
					int lastsimpleNum4 = s4.lastEntry().getValue().lastEntry().getValue().simpleNum;
					HashMap<Integer, CloseRoadMapData> dhd4 = new HashMap<Integer, CloseRoadMapData>();
					for (Map.Entry<Integer, TreeMap<Integer, CloseRoadMapData>> e4 : s4.entrySet()) {
						Integer nowangle4 = e4.getKey();
						// System.out.println(nowangle4);
						CloseRoadMapData data = e4.getValue().lastEntry().getValue();
						int nowsimpleNum4 = data.simpleNum;
						// 两个角度相差小于20°，去样本数多的那个
						if (Math.abs(lastAngle4 - nowangle4) < 20 || Math.abs(lastAngle4 - nowangle4) > 340) {
							if (nowsimpleNum4 > lastsimpleNum4) {
								dhd4.remove(lastAngle4);
								dhd4.put(nowangle4, data);
							}
						} else {
							dhd4.put(nowangle4, data);
						}
						lastAngle4 = nowangle4;
						lastsimpleNum4 = nowsimpleNum4;
					}

					int lastAngle5 = zhixnigLlist.lastEntry().getKey();
					int lastsimpleNum5 = zhixnigLlist.lastEntry().getValue().simpleNum;
					HashMap<Integer, CloseRoadMapData> dhd5 = new HashMap<Integer, CloseRoadMapData>();
					for (Map.Entry<Integer, CloseRoadMapData> e5 : zhixnigLlist.entrySet()) {
						Integer nowangle5 = e5.getKey();
						// System.out.println(nowangle4);
						CloseRoadMapData data = e5.getValue();
						int nowsimpleNum5 = data.simpleNum;
						// 两个角度相差小于20°，去样本数多的那个
						if (Math.abs(lastAngle5 - nowangle5) < 20 || Math.abs(lastAngle5 - nowangle5) > 340) {
							if (nowsimpleNum5 > lastsimpleNum5) {
								dhd5.remove(lastAngle5);
								dhd5.put(nowangle5, data);
							}
						} else {
							dhd5.put(nowangle5, data);
						}
						lastAngle5 = nowangle5;
						lastsimpleNum5 = nowsimpleNum5;
					}

					if (dhd3.size() == 4 && dhd4.size() == 4 && dhd5.size() == 4) {
						for (Map.Entry<Integer, CloseRoadMapData> eee3 : dhd3.entrySet()) {
							System.out.println(eee3.getValue().toString2());
						}
						// System.out.println("-----");
						for (Map.Entry<Integer, CloseRoadMapData> eee4 : dhd4.entrySet()) {
							System.out.println(eee4.getValue().toString2());
						}
						// System.out.println("-----");
						for (Map.Entry<Integer, CloseRoadMapData> eee5 : dhd5.entrySet()) {
							System.out.println(eee5.getValue().toString2());
						}
						// System.out.println("-----");
					}

				}
			}

		}
	}

	private static void sortRoad1(String outfile2, int maxnum, String readfile) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(outfile2)));
		String line = "";
		// TreeMap<Integer, HashSet<CloseRoadMapData>> treeMap = new
		// TreeMap<Integer, HashSet<CloseRoadMapData>>();
		// TreeMap<Integer, HashMap<String, HashMap<String, CloseRoadMapData>>>
		// dataMap = new TreeMap<Integer, HashMap<String, HashMap<String,
		// CloseRoadMapData>>>();
		// key:difpoint
		// value:key:dir:value :list<line>
		HashMap<String, HashMap<String, ArrayList<String>>> dataMap1 = new HashMap<String, HashMap<String, ArrayList<String>>>();
		while ((line = br.readLine()) != null) {
			CloseRoadMapData closeRoadMapData = CloseRoadMapData.parseData(line);
			// 掉头的去掉
			if (closeRoadMapData.flag == 3) {
				continue;
			}
			int simpleNum = closeRoadMapData.simpleNum;
			String difpoint = closeRoadMapData.nodeId;

			String[] arr = line.split(":");
			String pointID_angle_flag = arr[1] + "_" + arr[7] + "_" + arr[9];

			String key = difpoint;
			String flag = arr[9];
			if (dataMap1.containsKey(key)) {
				HashMap<String, ArrayList<String>> dataMp = dataMap1.get(key);

				if (dataMp.containsKey(flag)) {
					dataMp.get(flag).add(line);
				} else {
					ArrayList<String> dali = new ArrayList<String>();
					dali.add(line);
					dataMp.put(flag, dali);
				}
			} else {
				HashMap<String, ArrayList<String>> dataMp = new HashMap<String, ArrayList<String>>();
				ArrayList<String> dali = new ArrayList<String>();
				dali.add(line);
				dataMp.put(flag, dali);
				dataMap1.put(key, dataMp);
			}

		}
		TreeMap<Integer, HashMap<String, ArrayList<String>>> dataListMap = new TreeMap<Integer, HashMap<String, ArrayList<String>>>();
		for (Map.Entry<String, HashMap<String, ArrayList<String>>> entry : dataMap1.entrySet()) {
			String difPoint = entry.getKey();
			HashMap<String, ArrayList<String>> dataSp = entry.getValue();
			if (dataSp.size() != 3) {
				// System.out.println("buhege");
			} else {
				ArrayList<String> zhixing = dataSp.get("0");
				ArrayList<String> zuozhuan = dataSp.get("4");
				ArrayList<String> youzhuan = dataSp.get("5");

				ArrayList<String> zhixing1 = getCombine(zhixing);
				ArrayList<String> zuozhuan1 = getCombine(zuozhuan);
				ArrayList<String> youzhuan1 = getCombine(youzhuan);
				int zhixingsize = getDir(zhixing1);
				int zuozhuansize = getDir(zuozhuan1);
				int youzhuansize = getDir(youzhuan1);

				if (zhixingsize >= 4 && zuozhuansize >= 4 && youzhuansize >= 4) {
					ArrayList<String> d = new ArrayList<String>();
					int sumple = 0;
					for (String lineStr : zhixing1) {
						String[] lineArr = lineStr.split("\\:");
						String angle = lineArr[7];
						int simpleSize = Integer.parseInt(lineArr[8]);
						sumple += simpleSize;
						d.add(lineStr);
						// System.out.println(lineStr);
					}
					for (String lineStr : zuozhuan1) {
						String[] lineArr = lineStr.split("\\:");
						String angle = lineArr[7];
						int simpleSize = Integer.parseInt(lineArr[8]);
						d.add(lineStr);
						sumple += simpleSize;
						// System.out.println(lineStr);
					}
					for (String lineStr : youzhuan1) {
						String[] lineArr = lineStr.split("\\:");
						String angle = lineArr[7];
						int simpleSize = Integer.parseInt(lineArr[8]);
						d.add(lineStr);
						sumple += simpleSize;
						// System.out.println(lineStr);
					}
					if (dataListMap.containsKey(sumple)) {
						HashMap<String, ArrayList<String>> diflineMap = dataListMap.get(sumple);
						if (diflineMap.containsKey(difPoint)) {
							diflineMap.get(difPoint).addAll(d);
						} else {
							diflineMap.put(difPoint, d);
						}
					} else {
						HashMap<String, ArrayList<String>> diflineMap = new HashMap<String, ArrayList<String>>();
						diflineMap.put(difPoint, d);
						dataListMap.put(sumple, diflineMap);
					}

				}
			}
		}
		NavigableMap<Integer, HashMap<String, ArrayList<String>>> descMap = dataListMap.descendingMap();
		int num = 0;
		for (Entry<Integer, HashMap<String, ArrayList<String>>> e : descMap.entrySet()) {
			HashMap<String, ArrayList<String>> sss = e.getValue();
			for (Map.Entry<String, ArrayList<String>> ss : sss.entrySet()) {
				num++;
				if (num > maxnum) {
					break;
				}
				ArrayList<String> s = ss.getValue();
				// for (String li : s) {
				// System.out.println(li);
				// }
				// System.out.println("----------");
			}
		}

	}

	private static ArrayList<String> getCombine(ArrayList<String> zhixing) {
		// TODO Auto-generated method stub
		// 对于进出link一样的，取数据量多的那个
		ArrayList<String> dataList = new ArrayList<String>();
		HashMap<String, String> dataMAP = new HashMap<String, String>();
		for (String line : zhixing) {
			String[] lineArr = line.split("\\:");
			String angle = lineArr[7];
			String nodeId = lineArr[1];
			String inRoadId = lineArr[2];
			String outRoadId = lineArr[3];
			int simpleSize = Integer.parseInt(lineArr[8]);
			String keyy = inRoadId + "|" + outRoadId;
			if (dataMAP.containsKey(keyy)) {
				String[] lineArr1 = dataMAP.get(keyy).split("\\:");
				int befosimpleSize = Integer.parseInt(lineArr1[8]);
				if (befosimpleSize < simpleSize) {
					dataMAP.put(keyy, line);
				}
			} else {
				dataMAP.put(keyy, line);
			}
		}
		for (Map.Entry<String, String> ee : dataMAP.entrySet()) {
			dataList.add(ee.getValue());
		}
		return dataList;
	}

	private static int getDir(ArrayList<String> zhixing) {
		// TODO Auto-generated method stub
		HashSet<String> dataSet = new HashSet<String>();
		for (String line : zhixing) {
			String[] lineArr = line.split("\\:");
			String angle = lineArr[7];
			dataSet.add(angle);
		}
		return dataSet.size();
	}

	/**
	 * 
	 * @param outfile2
	 * @param readfile
	 * @throws IOException
	 * @throws ParseException
	 */
	private static void sortRoad(String outfile2, int maxnum, String readfile) throws IOException, ParseException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(outfile2)));
		String line = "";
		TreeMap<Integer, HashSet<CloseRoadMapData>> treeMap = new TreeMap<Integer, HashSet<CloseRoadMapData>>();
		TreeMap<Integer, HashMap<String, HashMap<String, CloseRoadMapData>>> dataMap = new TreeMap<Integer, HashMap<String, HashMap<String, CloseRoadMapData>>>();
		while ((line = br.readLine()) != null) {
			CloseRoadMapData closeRoadMapData = CloseRoadMapData.parseData(line);
			// 掉头的去掉
			if (closeRoadMapData.flag == 3) {
				continue;
			}
			int simpleNum = closeRoadMapData.simpleNum;
			String difpoint = closeRoadMapData.nodeId;

			String[] arr = line.split(":");
			String pointID_angle_flag = arr[1] + "_" + arr[7] + "_" + arr[9];
			String angle_flag = arr[7] + "_" + arr[9];
			// String flag=arr[9];
			// if (pointID_angle_flag.equals("561433_113_0")) {
			// System.out.println(line);
			//
			// // System.out.println("656556:110110:"+simpleNum+":"+line);
			// }
			if (dataMap.containsKey(simpleNum)) {
				HashMap<String, HashMap<String, CloseRoadMapData>> difMap = dataMap.get(simpleNum);
				HashMap<String, CloseRoadMapData> sss = difMap.get(difpoint);
				if (sss != null) {
					sss.put(angle_flag, closeRoadMapData);
				} else {
					sss = new HashMap<String, CloseRoadMapData>();
					sss.put(angle_flag, closeRoadMapData);
					difMap.put(difpoint, sss);
				}

			} else {
				HashMap<String, CloseRoadMapData> d1map = new HashMap<String, CloseRoadMapData>();
				d1map.put(angle_flag, closeRoadMapData);
				HashMap<String, HashMap<String, CloseRoadMapData>> difMap1 = new HashMap<String, HashMap<String, CloseRoadMapData>>();
				difMap1.put(difpoint, d1map);
				dataMap.put(simpleNum, difMap1);
			}
		}
		NavigableMap<Integer, HashMap<String, HashMap<String, CloseRoadMapData>>> descMap = dataMap.descendingMap();
		int num = 0;
		for (Map.Entry<Integer, HashMap<String, HashMap<String, CloseRoadMapData>>> we : descMap.entrySet()) {
			int simpleNum = we.getKey();
			HashMap<String, HashMap<String, CloseRoadMapData>> map1 = we.getValue();
			for (Map.Entry<String, HashMap<String, CloseRoadMapData>> we1 : map1.entrySet()) {
				String difpointMap = we1.getKey();

				HashMap<String, CloseRoadMapData> dirMap = we1.getValue();
				// 一个路口至少包含了9个方向的道路，左右转直行
				if (dirMap.size() >= 12) {
					num++;
					// System.out.println(dirMap.size() + "---------------");
					for (Map.Entry<String, CloseRoadMapData> we2 : dirMap.entrySet()) {
						String angle_flag = we2.getKey();
						CloseRoadMapData data = we2.getValue();
						// System.out.println(data.toString2());

						// if (num <= maxnum) {
						// System.out.println(data.toString2());
						// }
					}
				}

				// Set<String> keySet = dirMap.keySet();
				// boolean iszuozhuan = false;
				// boolean isyouzhuan = false;
				// boolean iszhixing = false;
				// for (String ss : keySet) {
				// String[] afarr = ss.split("\\_");
				// if (afarr[1].equals("4")) {
				// iszuozhuan = true;
				// }
				// if (afarr[1].equals("5")) {
				// isyouzhuan = true;
				// }
				// if (afarr[1].equals("0")) {
				// iszhixing = true;
				// }
				// }
				// if (iszuozhuan == true && isyouzhuan == true && iszhixing ==
				// true) {
				//
				// for (Map.Entry<String, CloseRoadMapData> we2 :
				// dirMap.entrySet()) {
				// String angle_flag = we2.getKey();
				// CloseRoadMapData data = we2.getValue();
				//
				// if (num <= maxnum) {
				// System.out.println(data.toString2());
				// }
				// }
				// }

			}
		}
		// BufferedReader br1 = new BufferedReader(new FileReader(new
		// File(readfile)));

	}

	private static void outToFile(HashMap<String, ArrayList<DifPointTotalMapInfo>> node_in_out_map, String outfile2) throws IOException {
		// TODO Auto-generated method stub
		BufferedWriter o = new BufferedWriter(new FileWriter(new File(outfile2)));
		for (Map.Entry<String, ArrayList<DifPointTotalMapInfo>> entry : node_in_out_map.entrySet()) {
			String nodeId_in_out_link = entry.getKey();
			ArrayList<DifPointTotalMapInfo> dataList = entry.getValue();
			for (DifPointTotalMapInfo data : dataList) {
				// System.out.println(data.toString21());
				o.write(data.toString1());
				o.newLine();
				o.flush();
			}

		}
		o.close();
	}

	private static void getLinkLenDisGeoInfo(HashMap<String, ArrayList<DifPointTotalMapInfo>> node_in_out_map, HashMap<String, Integer> linklen, HashMap<String, Geometry> linkGeo, HashMap<String, String> linkMeshIds, HashMap<String, String> linkAngle) throws ParseException {
		// TODO Auto-generated method stub
		for (Map.Entry<String, ArrayList<DifPointTotalMapInfo>> entry : node_in_out_map.entrySet()) {
			String nodeId_in_out_link = entry.getKey();
			ArrayList<DifPointTotalMapInfo> dataList = entry.getValue();
			for (DifPointTotalMapInfo data : dataList) {
				String enterLink = data.enter_out_DifPoint_Link.split("\\|")[0];
				String outerLink = data.enter_out_DifPoint_Link.split("\\|")[1];
				String[] linkSeq = data.linkArr.split("\\|");
				StringBuffer linkLenuf = new StringBuffer();
				int difIedex = 0;
				HashSet<String> meshIds = new HashSet<String>();
				for (int i = 0; i < linkSeq.length; i++) {
					int len = linklen.get(linkSeq[i]);
					linkLenuf.append(linkSeq[i]);
					linkLenuf.append(":");
					linkLenuf.append(len);
					if (i != linkSeq.length - 1) {
						linkLenuf.append("|");
					}
					// if ("01".equals(data.dir) || "11".equals(data.dir)) {
					// if (linkSeq[i].equals(outerLink)) {
					// difIedex = i;
					// }
					// } else {
					// if (linkSeq[i].equals(enterLink)) {
					// difIedex = i;
					// }
					// }
					if (linkSeq[i].equals(outerLink)) {
						difIedex = i;
					}
					String[] meshs = linkMeshIds.get(linkSeq[i]).split("\\|");
					for (String meshID : meshs) {
						meshIds.add(meshID);
					}

				}
				String[] dis = data.LinkDisArr.split("\\|");
				if (data.roadId.equals("12645748_12566710")) {
					System.out.println("断点处");
				}
				data.dif_Point_Dis = Integer.parseInt(dis[difIedex]);
				data.difPoint_startDis = data.totalLength - data.dif_Point_Dis;
				data.difPoint_angle = data.straight_angle;
				data.linksLen = linkLenuf.toString();
				data.meshIdSeq = getMeshIds(meshIds);
				getRoadGEO(data, linkGeo);
				getMinMaxAzimuth(data, linkAngle);
			}
			// 计算minSuitableDisFromLastLink\maxSuitableDisFromLastLink
			if (dataList.size() > 1) {
				// System.out.println("有分叉！");
				// 获取数据的lastSameLink和firstSameLink
				for (DifPointTotalMapInfo data : dataList) {
					String firstSameLink = data.firstSameLink;
					String lastSameLink = data.lastSameLink;
					String[] linkSeq = data.linkArr.split("\\|");
					int minindex = 0;
					int maxindex = 0;
					for (int i = 0; i < linkSeq.length; i++) {
						if (linkSeq[i].equals(lastSameLink)) {
							minindex = i;
							continue;
						}
					}
					for (int i = 0; i < linkSeq.length; i++) {
						if (linkSeq[i].equals(firstSameLink)) {
							maxindex = i;
							continue;
						}
					}
					String[] disSeq = data.LinkDisArr.split("\\|");

					int maxSuitableDisFromLastLink = data.totalLength - Integer.parseInt(disSeq[maxindex]);
					data.maxSuitableDisFromLastLink = maxSuitableDisFromLastLink;
					if (minindex == disSeq.length - 1) {
						int minSuitableDisFromLastLink = 0;

						data.minSuitableDisFromLastLink = minSuitableDisFromLastLink;
						data.samelinkLen = maxSuitableDisFromLastLink - minSuitableDisFromLastLink;
					} else {
						int minSuitableDisFromLastLink = data.totalLength - Integer.parseInt(disSeq[minindex + 1]);
						data.minSuitableDisFromLastLink = minSuitableDisFromLastLink;

						data.samelinkLen = maxSuitableDisFromLastLink - minSuitableDisFromLastLink;
					}

				}

			} else {
				// 说明单方向上没有分叉的情况
				dataList.get(0).maxSuitableDisFromLastLink = dataList.get(0).totalLength;
				dataList.get(0).minSuitableDisFromLastLink = 0;
				dataList.get(0).samelinkLen = dataList.get(0).totalLength - 0;
			}

		}
	}

	/**
	 * 得到link序列最大方位角和最小方位角
	 * 
	 * @param data
	 * @param linkAngle
	 */
	private static void getMinMaxAzimuth(DifPointTotalMapInfo data, HashMap<String, String> linkAngle) {
		// TODO Auto-generated method stub
		String[] linkSeq = data.linkArr.split("\\|");
		String intAngle = linkAngle.get(linkSeq[0]);
		String outAngle = linkAngle.get(linkSeq[linkSeq.length - 1]);
		String[] ang1 = intAngle.split("\\_");
		String[] ang2 = outAngle.split("\\_");
		// LineMerger lineMerger = new LineMerger();
		// List<Geometry> list = new ArrayList<Geometry>();
		// TreeMap<Double, Double> minmaxMap = new TreeMap<Double, Double>();
		// for (String link : linkSeq) {
		// String minmaxAngle = linkAngle.get(link);
		// if (minmaxAngle != null) {
		// String[] minmax = minmaxAngle.split("\\_");
		// double minangle = Double.parseDouble(minmax[0]);
		// double maxangle = Double.parseDouble(minmax[1]);
		// minmaxMap.put(minangle, minangle);
		// minmaxMap.put(maxangle, maxangle);
		// }
		// }
		data.enter_Azimuth = Double.parseDouble(ang1[0]);
		data.out_Azimuth = Double.parseDouble(ang2[1]);

	}

	/**
	 * 获取link序列形状
	 * 
	 * @param data
	 * @param linkGeo
	 * @return
	 * @throws ParseException
	 */
	private static void getRoadGEO(DifPointTotalMapInfo data, HashMap<String, Geometry> linkGeo) throws ParseException {
		// TODO Auto-generated method stub
		String[] linkSeq = data.linkArr.split("\\|");
		LineMerger lineMerger = new LineMerger();
		List<Geometry> list = new ArrayList<Geometry>();
		for (String link : linkSeq) {
			Geometry geo = linkGeo.get(link);
			if (geo != null) {
				list.add(geo);
			}
		}
		lineMerger.add(list);
		Collection<Geometry> mergerLineStrings = lineMerger.getMergedLineStrings();
		for (Geometry dpr : mergerLineStrings) {
			Geometry g1 = new WKTReader().read(dpr.toString());
			Geometry buffer = g1.buffer(bufferAreaDistance);
			// BufferOp bufOp = new BufferOp(g);
			// bufOp.setQuadrantSegments(0);
			// System.out.println(buffer.toString2());
			data.roadGeo = buffer;

		}
	}

	/**
	 * 获取所有的mesh
	 * 
	 * @param meshIds
	 * @return
	 */
	private static String getMeshIds(HashSet<String> meshIds) {
		// TODO Auto-generated method stub
		StringBuffer sf = new StringBuffer();
		for (String meshId : meshIds) {
			sf.append(meshId);
			sf.append("|");
		}
		return sf.toString().substring(0, sf.toString().length() - 1);
	}

	private static void getNode_in_out_map(HashMap<String, ArrayList<DifPointTotalMapInfo>> node_in_out_map) {
		// TODO Auto-generated method stub
		for (Map.Entry<String, ArrayList<DifPointTotalMapInfo>> entry : node_in_out_map.entrySet()) {
			String nodeId_in_out_link = entry.getKey();
			ArrayList<DifPointTotalMapInfo> dataList = entry.getValue();
			if (dataList.size() > 1) {
				getLastSameLink(dataList);
			} else {
				// 说明单方向上没有分叉的情况
			}
		}
	}

	/**
	 * 得到相同方向但是有分叉的公共link
	 * 
	 * @param dataList
	 * @return
	 */
	private static void getLastSameLink(ArrayList<DifPointTotalMapInfo> dataList) {
		// TODO Auto-generated method stub
		String maxSameStr = dataList.get(0).linkArr;
		for (int i = 1; i < dataList.size(); i++) {
			String linkSeqbef = dataList.get(i).linkArr;
			maxSameStr = getCommonStrLength(maxSameStr, linkSeqbef);

		}
		String[] link = maxSameStr.split("\\|");
		String lastSameLink = link[link.length - 1];
		String firstSameLink = link[0];
		for (int i = 0; i < dataList.size(); i++) {
			dataList.get(i).lastSameLink = lastSameLink;
			dataList.get(i).firstSameLink = firstSameLink;
		}
	}

	private static String getCommonStrLength(String str1, String str2) {
		// TODO Auto-generated method stub
		// 12644678|12644677|500679|15086815|15086814|493704|448224
		// 12644678|12644677|500679|15086815|15086814|493704|448225
		String[] arr1 = str1.split("\\|");
		String[] arr2 = str2.split("\\|");
		String line = "";
		for (int i = 0; i < arr1.length; i++) {
			String str = line + arr1[i];
			if (str2.contains(str)) {
				line += arr1[i];
				if (i != arr1.length - 1) {
					line += "|";
				}
			} else {
				continue;
			}
		}
		if (line.endsWith("|")) {
			line = (String) line.subSequence(0, line.length() - 1);
		}
		return line;
	}

	/**
	 * 按照node+直行角度+进出口linkid分好组后，每一组中，如果进入link一样则为汇出，出去link一样则为汇入
	 * 
	 * @param sameNodeRoadsMap
	 */
	private static HashMap<String, ArrayList<DifPointTotalMapInfo>> getInOrOutInfo(HashMap<String, ArrayList<DifPointTotalMapInfo>> sameNodeRoadsMap) {
		// TODO Auto-generated method stub
		HashMap<String, ArrayList<DifPointTotalMapInfo>> dataMap = new HashMap<String, ArrayList<DifPointTotalMapInfo>>();
		for (Map.Entry<String, ArrayList<DifPointTotalMapInfo>> entry : sameNodeRoadsMap.entrySet()) {
			String nodeId_angle_inoutLink = entry.getKey();
			ArrayList<DifPointTotalMapInfo> dataList = entry.getValue();
			// if (dataList.size() > 1) {
			// 设置方向
			setDirFlag(dataList);
			for (DifPointTotalMapInfo data : dataList) {
				String key = nodeId_angle_inoutLink+"_"+data.enter_out_DifPoint_Link;
				ArrayList<DifPointTotalMapInfo> value = dataMap.get(key);
				if (value == null) {
					value = new ArrayList<DifPointTotalMapInfo>();
					value.add(data);
					dataMap.put(key, value);
				} else {
					value.add(data);
					// System.out.println("1有分叉口");
				}
			}

		}
		return dataMap;
	}

	/**
	 * 设置每条道路的同行方向
	 * 
	 * @param dirFlag
	 * @param dataList
	 */
	private static void setDirFlag(ArrayList<DifPointTotalMapInfo> dataList) {
		// TODO Auto-generated method stub
		boolean hasRight = false, hasLeft = false, hasStraight = false;
		for (DifPointTotalMapInfo data : dataList) {
			// 02表示掉头,20表示直行，30表示左转，03表示右转
			if (data.flag == 0) {
				hasStraight = true;
				// data.dir = "20";
			}
			if (data.flag == 4) {
				hasLeft = true;
				// data.dir = "30";
			}
			if (data.flag == 5) {
				hasRight = true;
				// data.dir = "03";
			}
		}
		String dir = "";
		// 相同路口+相同直行方向，有左转和右转
		if ((hasRight) && (hasLeft)) {
			dir = "03";
		}
		// 相同路口+相同直行方向，只有右转
		if ((hasRight) && (!hasLeft)) {
			dir = "20";
		}
		// 相同路口+相同直行方向，只有左转
		if ((!hasRight) && (hasLeft)) {
			dir = "02";
		}
		// 相同路口+相同直行方向，既没左转和既没右转
		if ((!hasRight) && (!hasLeft)) {
			dir = "30";
		}
		for (DifPointTotalMapInfo data : dataList) {
			data.dir = dir;
		}
	}

	/**
	 * 获取同行标志是汇入还是汇出:00表示左出，10表示右出，01表示左入，11表示右入
	 * 
	 * @param dataList
	 * @return
	 */
	private static String getDirFlag(ArrayList<DifPointTotalMapInfo> dataList) {
		// TODO Auto-generated method stub
		String enterLink = dataList.get(0).enter_out_DifPoint_Link.split("\\|")[0];
		double stringAngle = 0;
		for (DifPointTotalMapInfo d : dataList) {
			if (d.flag == 0) {
				stringAngle = d.straight_angle;
			}
		}
		for (DifPointTotalMapInfo d : dataList) {
			if (d.flag != 0) {
				d.straight_angle = stringAngle;
			}
		}
		int flag = dataList.get(0).flag == 0 ? 0 : dataList.get(0).flag;
		for (DifPointTotalMapInfo d : dataList) {
			flag = ((flag - d.flag) >= 0 ? flag : d.flag);
			// if
			// (!(d.enter_out_DifPoint_Link.split("\\|")[0].equals(enterLink)))
			// {
			// if (flag == 4) {
			// // zuoru
			// return "01";
			// }
			// if (flag == 5) {
			// // youru
			// return "11";
			// }
			// }
		}
		if (flag == 4) {
			// zuochu
			return "00";
		}
		if (flag == 5) {
			// youchu
			return "10";
		}
		return null;
	}

	/**
	 * nodeid+直行角度分组+进出linkid
	 * 
	 * @param sameNodeRoadsMap1
	 */
	private static void getSameNodeRoads(String closeroadmapdatafile, HashMap<String, ArrayList<DifPointTotalMapInfo>> sameNodeRoadsMap, int simplesmallnum,int roadClass) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(closeroadmapdatafile)));
		String line = "";
		HashSet<String> ss = new HashSet<String>();
		while ((line = br.readLine()) != null) {
			CloseRoadMapData closeRoadMapData = CloseRoadMapData.parseData(line);
			// 掉头的去掉
			if (closeRoadMapData.flag == 3) {
				continue;
			}
			// 样本数小于simplesmallnum的舍弃
			if (closeRoadMapData.simpleNum < simplesmallnum)
				continue;
			// link序列有重复的舍弃
			if (hasSameLink(closeRoadMapData.linkseq)) {
				System.out.println(closeRoadMapData.linkseq);
				continue;
			}

			String nodeId = closeRoadMapData.nodeId;
			int angle = closeRoadMapData.angel;
			String key = nodeId + "_" + angle;// + "_" + closeRoadMapData.inRoadId + "_" + closeRoadMapData.outRoadId;
//			String key1 = nodeId + "_" + angle + "_" + closeRoadMapData.inRoadId + "_" + closeRoadMapData.outRoadId;

			ArrayList<DifPointTotalMapInfo> dataList = sameNodeRoadsMap.get(key);
			DifPointTotalMapInfo difData = new DifPointTotalMapInfo(closeRoadMapData,roadClass);
			if (dataList == null) {
				dataList = new ArrayList<DifPointTotalMapInfo>();
				dataList.add(difData);
				sameNodeRoadsMap.put(key, dataList);
			} else {
				boolean isSame = false;
				for (DifPointTotalMapInfo data : dataList) {
					if (difData.linkArr.contains(data.linkArr)) {
						data = difData;
						isSame = true;
					}
				}
				if (!isSame)
					dataList.add(difData);
			}
			
		}
	}

	/**
	 * 判断link序列中是否含有相同的link
	 * 
	 * @param linkseq
	 * @return
	 */
	private static boolean hasSameLink(String linkseq) {
		// TODO Auto-generated method stub
		HashSet<String> linkSet = new HashSet<String>();
		String[] linkSeq = linkseq.split("\\|");
		for (String link : linkSeq) {
			if (linkSet.contains(link)) {
				return true;
			} else {
				linkSet.add(link);
			}
		}
		return false;
	}

	private static void getLinkGeo(String rGMapFile, HashMap<String, Geometry> linkGeo, HashMap<String, Integer> linklen, HashMap<String, String> linkMeshIds, int cellsize, HashMap<String, String> linkAngle) throws IOException, ParseException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(rGMapFile)));
		String line = "";
		// HashMap<String, Geometry> getLinkGeo = new HashMap<String,
		// Geometry>();
		// HashMap<String, String> getLinkLen = new HashMap<String, String>();
		// HashMap<String, String> getlinkMesh = new HashMap<String, String>();
		while ((line = br.readLine()) != null) {
			R_G_DATA data = R_G_DATA.parseData(line);
			linkGeo.put(data.linkId, data.linkSeqGEO);
			String inOutAngle = getAngle(data.linkSeqGEO.toString());
			linkAngle.put(data.linkId, inOutAngle);
			linklen.put(data.linkId, Integer.parseInt(data.linklength));
			getMeshIds(data, cellSize, linkMeshIds);
		}
	}

	/**
	 * 得到link头两个点的点的方位角、和最后两个点的方位角
	 * 
	 * @param string
	 * @return
	 */
	private static String getAngle(String linkSeqGEO) {
		// TODO Auto-generated method stub
		// System.out.println(linkSeqGEO);
		String linkAngleArr1 = linkSeqGEO.replace("LINESTRING", "").trim();
		String linkAngleArr2 = linkAngleArr1.replace("(", "").trim();
		String linkAngleArr3 = linkAngleArr2.replace(")", "").trim();
		String[] linkAngleArr = linkAngleArr3.split(",");

		// TreeMap<Double, Double> treeMap = new TreeMap<Double, Double>();
		// for (int i = 0; i < linkAngleArr.length - 1; i++) {
		// String[] lonlatArr = linkAngleArr[i].trim().split(" ");
		// double nowlon = Double.parseDouble(lonlatArr[0]);
		// double nowlat = Double.parseDouble(lonlatArr[1]);
		// String[] aftlonlatArr = linkAngleArr[i + 1].trim().split(" ");
		// double aftnowlon = Double.parseDouble(aftlonlatArr[0]);
		// double aftnowlat = Double.parseDouble(aftlonlatArr[1]);
		// double azimuth = MapUtil.azimuth(nowlon, nowlat, aftnowlon,
		// aftnowlat);
		// treeMap.put(azimuth, azimuth);
		// }
		// for (int i = 0; i < 2; i++) {
		// String[] lonlatArr = linkAngleArr[i].trim().split(" ");
		// double nowlon = Double.parseDouble(lonlatArr[0]);
		// double nowlat = Double.parseDouble(lonlatArr[1]);
		// String[] aftlonlatArr = linkAngleArr[i + 1].trim().split(" ");
		// double aftnowlon = Double.parseDouble(aftlonlatArr[0]);
		// double aftnowlat = Double.parseDouble(aftlonlatArr[1]);
		// double azimuth = MapUtil.azimuth(nowlon, nowlat, aftnowlon,
		// aftnowlat);
		// treeMap.put(azimuth, azimuth);
		// }
		String[] lonlatArr = linkAngleArr[0].trim().split(" ");
		double nowlon = Double.parseDouble(lonlatArr[0]);
		double nowlat = Double.parseDouble(lonlatArr[1]);
		String[] aftlonlatArr = linkAngleArr[1].trim().split(" ");
		double aftnowlon = Double.parseDouble(aftlonlatArr[0]);
		double aftnowlat = Double.parseDouble(aftlonlatArr[1]);
		double azimuth = MapUtil.azimuth(nowlon, nowlat, aftnowlon, aftnowlat);

		String[] lonlatArr1 = linkAngleArr[linkAngleArr.length - 2].trim().split(" ");
		double nowlon1 = Double.parseDouble(lonlatArr1[0]);
		double nowlat1 = Double.parseDouble(lonlatArr1[1]);
		String[] aftlonlatArr1 = linkAngleArr[linkAngleArr.length - 1].trim().split(" ");
		double aftnowlon1 = Double.parseDouble(aftlonlatArr1[0]);
		double aftnowlat1 = Double.parseDouble(aftlonlatArr1[1]);
		double azimuth1 = MapUtil.azimuth(nowlon1, nowlat1, aftnowlon1, aftnowlat1);
		// treeMap.put(azimuth, azimuth);
		// String minmax = treeMap.firstKey() + "_" + treeMap.lastKey();
		String enterOut = azimuth + "_" + azimuth1;
		return enterOut;
	}

	/**
	 * 获取link所属meshid
	 * 
	 * @param data
	 * @param cellsize2
	 * @param getlinkMesh
	 * @return
	 */
	private static void getMeshIds(R_G_DATA data, int cellsize2, HashMap<String, String> getlinkMesh) {
		// TODO Auto-generated method stub
		HashSet<String> getMeshIds = new HashSet<String>();
		String line = data.linkSeqGEO.toString().replace("(", "").replace(")", "").replace("LINESTRING", "");
		String[] arr = line.split(",");
		for (String lonlat : arr) {
			String[] ll = lonlat.trim().split(" ");
			double lon = Double.parseDouble(ll[0]);
			double lat = Double.parseDouble(ll[1]);
			String meshId = MapUtil.findCell(lon, lat, 200);
			getMeshIds.add(meshId);
		}
		StringBuffer meshs = new StringBuffer();
		for (String meshId : getMeshIds) {
			meshs.append(meshId);
			meshs.append("|");
		}
		getlinkMesh.put(data.linkId, meshs.toString());
	}

	private static void test(String closeroadmapdatafile2) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(closeroadmapdatafile2)));
		String line = "";
		HashSet<String> ss = new HashSet<String>();
		TreeMap<Integer, ArrayList<String>> roadMap = new TreeMap<Integer, ArrayList<String>>();
		while ((line = br.readLine()) != null) {
			CloseRoadMapData closeRoadMapData = CloseRoadMapData.parseData(line);
			int number = closeRoadMapData.simpleNum;
			if (roadMap.containsKey(number)) {
				ArrayList<String> value = roadMap.get(number);
				value.add(line);
			} else {
				ArrayList<String> value = new ArrayList<String>();
				value.add(line);
				roadMap.put(number, value);
			}
		}
		for (Map.Entry<Integer, ArrayList<String>> entry : roadMap.entrySet()) {
			int simpleNum = entry.getKey();
			ArrayList<String> value = entry.getValue();
			for (String lines : value) {
				// System.out.println(simpleNum + "," + lines);
			}
		}
		br.close();
		br = null;
	}

	/**
	 * 对单个红绿灯10字路口单方向单转向只保留一个，去掉重复记录数
	 * 
	 * @throws IOException
	 */
	private static void dataSplit(String inputfilefolder2, String closeroadmapdatafile2) throws IOException {
		// TODO Auto-generated method stub
		File[] files = new File(inputfilefolder2).listFiles();
		HashMap<String, String> dataMap = new HashMap<>();
		for (File file : files) {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line = "";
			while ((line = br.readLine()) != null) {
				String[] arr = line.split(":");
				String pointID_angle_flag = arr[1] + "_" + arr[7] + "_" + arr[9];
				dataMap.put(pointID_angle_flag, line);
			}
			br.close();
			br = null;
		}
		BufferedWriter write = new BufferedWriter(new FileWriter(new File(closeroadmapdatafile2)));
		for (Map.Entry<String, String> entry : dataMap.entrySet()) {
			write.write(entry.getValue());
			write.newLine();
			write.flush();
		}
		write.close();
		write = null;
	}

	/**
	 * 将多个文件数据整合到一个文件中
	 * 
	 * @param outfile2
	 * 
	 * @throws IOException
	 */
	private static void intergrationMapAttr(String closeroadturnattrfile2, String closeroadlinklengthfile2, String outfile2) throws IOException {
		// TODO Auto-generated method stub
		/** key:pointID_flag_angle;value:形狀 **/
		HashMap<String, Geometry> difPointEndRoadBuffer = DifPointRoadGeoService.getDifPointRoadBuffer(closeRoadMapDataFile, R_G_Map_File);
		BufferedReader br1 = new BufferedReader(new FileReader(new File(closeroadturnattrfile2)));
		BufferedReader br2 = new BufferedReader(new FileReader(new File(closeroadlinklengthfile2)));
		BufferedWriter write = new BufferedWriter(new FileWriter(new File(outfile2)));
		String line2 = "";
		HashMap<String, String> linkLenMap2 = new HashMap<>();
		while ((line2 = br2.readLine()) != null) {
			String[] arr = line2.split(",");
			linkLenMap2.put(arr[0], arr[1]);
		}
		br2.close();
		br2 = null;
		String line1 = "";

		while ((line1 = br1.readLine()) != null) {
			CloseRoadMapData data = CloseRoadMapData.parseData1(line1);
			String[] mapData = line1.split(",");
			String difPointId = data.nodeId;
			int angle = data.angel;
			int dir = data.flag;
			String key = difPointId + "_" + dir + "_" + angle;
			String linkLenSeq = linkLenMap2.get(key);
			if (difPointEndRoadBuffer.get(key) == null) {
				continue;
			}
			String roadGeoInfo = difPointEndRoadBuffer.get(key).toString();
			String[] lonlatArr = roadGeoInfo.replace("POLYGON ((", "").replace("))", "").replace(")", "").replace("(", "").split(",");
			Set<String> meshSet = new HashSet<>();
			for (int i = 0; i < lonlatArr.length; i++) {

				double lon = Double.parseDouble(lonlatArr[i].trim().split(" ")[0]);
				double lat = Double.parseDouble(lonlatArr[i].trim().split(" ")[1]);
				int meshId = MapUtil.findMesh(lon, lat);
				String meshMinId = MapUtil.findCell(lon, lat, 100);
				meshSet.add(meshMinId);

			}

			String roadId = data.roadId;
			String linkIdSeq = data.linkseq;
			String linkDisSeqFromLeft = data.disSeq;
			int totalLen = data.totalLen;
			int befDis = data.beforeRoadDis;
			String bef_aft_difPoint_link = data.inRoadId + "|" + data.outRoadId;
			String nodeId = data.nodeId;
			double lon = data.lon;
			double lat = data.lat;
			int straightAngle = data.angel;
			int flag = data.flag;
			int difPointDisFormRight = data.afterRoadDis;
			String turnFlag = data.turnFlag;
			String linkLenSeq1 = linkLenSeq;
			straightAngle = data.angel;
			String meshIds = "";
			for (String meshId : meshSet) {
				meshIds += meshId + "|";
			}
			String outLine = roadId + "," + linkIdSeq + "," + linkDisSeqFromLeft + "," + totalLen + "," + befDis + "," + bef_aft_difPoint_link + "," + nodeId + "," + lon + "," + lat + "," + straightAngle + "," + flag + "," + difPointDisFormRight + "," + turnFlag + "," + linkLenSeq1 + "," + straightAngle + "," + meshIds + "," + roadGeoInfo;
			write.write(outLine);
			write.newLine();
			write.flush();
		}
		write.close();
		write = null;
		br1.close();
		br1 = null;
	}

	/**
	 * 生成link长度对应表
	 * 
	 * @throws IOException
	 * @throws NumberFormatException
	 */
	private static void getLinkLen(String inputFile, String outputFile) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(inputFile)));
		BufferedWriter write = new BufferedWriter(new FileWriter(new File(outputFile)));
		String line = "";
		while ((line = br.readLine()) != null) {
			// System.out.println(line);
			CloseRoadMapData closeRoadData = CloseRoadMapData.parseData1(line);
			String[] linkArr = closeRoadData.linkseq.split("\\|");
			int totalLength = closeRoadData.totalLen;
			String[] LinkDisArr = closeRoadData.disSeq.split("\\|");
			String dif_Point_Id = closeRoadData.nodeId;
			int angle = closeRoadData.angel;
			int flag = closeRoadData.flag;
			int dataLen = linkArr.length;

			String outLine = dif_Point_Id + "_" + flag + "_" + angle + ",";
			String firstLinkId = linkArr[0];
			String firstLinkLen = LinkDisArr[1];
			outLine += firstLinkId + ":" + firstLinkLen + "|";

			for (int i = 1; i < dataLen - 1; i++) {
				String linkId = linkArr[i];
				int linkLen = Integer.parseInt(LinkDisArr[i + 1]) - Integer.parseInt(LinkDisArr[i]);
				outLine += linkId + ":" + linkLen + "|";
			}
			String lastLinkId = linkArr[dataLen - 1];
			int lastLinkLen = totalLength - Integer.parseInt(LinkDisArr[dataLen - 1]);
			outLine += lastLinkId + ":" + lastLinkLen + "|";
			// System.out.println(outLine);
			write.write(outLine);
			write.newLine();
			write.flush();
		}
	}

	/**
	 * 获取转向标志： 00表示左出，10表示右出，02掉头,20直行
	 */
	private static void getCloseRoadTurnAttr(String inPutFile, String outPutFile) throws IOException {
		// TODO Auto-generated method stub

		HashMap<String, TreeMap<Integer, String>> sameDifPointMap = new HashMap<>();
		BufferedReader br = new BufferedReader(new FileReader(inPutFile));
		BufferedWriter write = new BufferedWriter(new FileWriter(new File(outPutFile)));
		String line = "";
		while ((line = br.readLine()) != null) {
			CloseRoadMapData closeRoadData = CloseRoadMapData.parseData(line);
			/**
			 * 根据转向标志位，标志位: 0:直行3:掉头4: 左转5: 右转;判断转向标志
			 * 00表示左出，10表示右出，02表示掉头,20表示直行
			 **/
			int flag = closeRoadData.flag;
			String turnFlag = "";
			switch (flag) {
			case 0:
				turnFlag = "20";
				break;
			case 3:
				turnFlag = "02";
				break;
			case 4:
				turnFlag = "00";
				break;
			case 5:
				turnFlag = "10";
				break;
			default:
				break;
			}
			closeRoadData.turnFlag = turnFlag;
			// System.out.println(closeRoadData.toString21());
			write.write(closeRoadData.toString1());
			write.newLine();
			write.flush();
		}
	}

}
